package com.aduerr.v1shouyin.mvp.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.entity.OrderGetPosOrderLEntity;
import com.aduerr.v1shouyin.print.PrinterWrapper;
import com.aduerr.v1shouyin.unioncard.BaseUnionActivity;
import com.aduerr.v1shouyin.util.SharedPreferencesUtils;
import com.aduerr.v1shouyin.util.ToastUtils;
import com.orhanobut.hawk.Hawk;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RefoundRecordItemDetailActivity extends BaseUnionActivity {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.iv_icon_type)
    ImageView ivIconType;
    @BindView(R.id.tv_refoundmoney)
    TextView tvRefoundmoney;
    @BindView(R.id.tv_refoundmoney_type)
    TextView tvRefoundmoneyType;
    @BindView(R.id.tv_bufentuikaun)
    TextView tvBufentuikaun;
    @BindView(R.id.tv_ordermoney)
    TextView tvOrdermoney;
    @BindView(R.id.tv_zhifubao)
    TextView tvZhifubao;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_dingdanhao)
    TextView tvDingdanhao;
    @BindView(R.id.tv_shouyinyuan)
    TextView tvShouyinyuan;
    @BindView(R.id.tv_beizhu)
    TextView tvBeizhu;
    @BindView(R.id.tv_print_note)
    TextView tvPrintNote;
    @BindView(R.id.tv_shop)
    TextView tvShop;
    @BindView(R.id.tv_refund_beizhu)
    TextView tvRefundBeizhu;


    private OrderGetPosOrderLEntity.DataBean.ListBean datebean;
    private String[] content = new String[20];


    @Override
    protected int getLayoutId() {
        return R.layout.activity_refound_item_detail;
    }

    @Override
    protected void initBase() {
        super.initView();
        ButterKnife.bind(this);
        initdate();
    }


    private void initdate() {
        tvTitle.setText("退款详情");
        //intent.putExtra("date", inlist.get(position));\

        datebean =
                (OrderGetPosOrderLEntity.DataBean.ListBean) getIntent().getSerializableExtra("date");
        if(datebean != null) {
            String paytype = datebean.getPaytype();
            if(TextUtils.equals(paytype,
                    this.getResources().getString(R.string.text_dingdan_weixin))) {
                ivIconType.setImageResource(R.drawable.img_order_wechat);
            } else if(TextUtils.equals(paytype,
                    this.getResources().getString(R.string.text_dingdan_zhifubao))) {
                ivIconType.setImageResource(R.drawable.img_order_zhifubao);
            } else if(TextUtils.equals(paytype,
                    this.getResources().getString(R.string.text_dingdan_QQqianbao))) {
                ivIconType.setImageResource(R.drawable.img_order_qq);
            } else if(TextUtils.equals(paytype,
                    this.getResources().getString(R.string.text_dingdan_shuaka))) {
                ivIconType.setImageResource(R.drawable.img_order_shuaka);
            } else if(TextUtils.equals(paytype,
                    this.getResources().getString(R.string.text_dingdan_xianjin))) {
                ivIconType.setImageResource(R.drawable.img_order_cash);
            } else if(TextUtils.equals(paytype,
                    this.getResources().getString(R.string.text_dingdan_other))) {
                ivIconType.setImageResource(R.drawable.img_order_cash);
            } else if(TextUtils.equals(paytype, "云闪付")) {
                ivIconType.setImageResource(R.drawable.img_order_ysf);
            }else if(TextUtils.equals(paytype, "数币") || TextUtils.equals(paytype, "数字人民币")) {
                ivIconType.setImageResource(R.drawable.icon_digital_cirlce_big);
            }
            if(datebean.getPaystate().equals("已退款")){
                tvRight.setVisibility(View.VISIBLE);
            }


            content[0] = datebean.getOrderid();//订单编号
            content[1] = datebean.getShopname();//门店
            content[2] = datebean.getSiteusername();//收银员
            content[3] = datebean.getPaytype();//支付方式
            content[4] = datebean.getPaystate();//支付状态
            content[5] = datebean.getPaytime();//支付时间
            content[6] = datebean.getOrdermoney();//订单金额
            content[7] = datebean.getRealpay();//实收金额
            content[8] = datebean.getRefund();//退款金额
            content[9] = datebean.getOrdernote();//订单备注
            content[10] = datebean.getRefundreason();//退款备注
            content[11] = datebean.getRefundtime();//退款时间
            content[12] = Hawk.get("company");//账号公司
            content[13] = datebean.getUseraccount();//用户账号 不一定存在
            content[14] = datebean.getOutorderid();//不一定存在
            content[15] = datebean.getSelleryouhui();//不一定存在
            content[16] = datebean.getYouhui();//不一定存在
            content[17] = datebean.getRefundusername();//不一定存在



            tvRefoundmoney.setText(datebean.getRefund());
            tvRefoundmoneyType.setText(datebean.getPaystate());
            tvBufentuikaun.setText(datebean.getRefund());
            tvOrdermoney.setText(datebean.getOrdermoney());
            tvZhifubao.setText(datebean.getUseraccount());
            tvTime.setText(datebean.getPaytime());
            tvDingdanhao.setText(datebean.getOrderid());
            tvShouyinyuan.setText(datebean.getSiteusername());
            tvBeizhu.setText(datebean.getOrdernote());
            tvShop.setText(datebean.getShopname());
            tvRefundBeizhu.setText(datebean.getRefundreason());

        }
    }

    @OnClick({R.id.img_back, R.id.tv_print_note, R.id.tv_right})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_print_note:
                // TODO: 2017/12/12 0012  打印小票
                int printpage =
                        (int) SharedPreferencesUtils.getParam(RefoundRecordItemDetailActivity.this, "printpage", 1);
                if(printpage == 0) {
                    ToastUtils.showToast(RefoundRecordItemDetailActivity.this,
                            "打印张数设置为不打印,请先设置打印张数!!");
                    return;
                }

                if(content != null && datebean != null) {
                    String paytype = datebean.getPaytype();
                    Bitmap bitmap = null;
                    if(paytype.equals("微信")) {
                        bitmap = BitmapFactory.decodeResource(getResources(),
                                R.drawable.print_w_refound);
                    } else if(paytype.equals("支付宝")) {
                        bitmap = BitmapFactory.decodeResource(getResources(),
                                R.drawable.print_z_refound);
                    } else if(paytype.equals("云闪付")) {
                        bitmap = BitmapFactory.decodeResource(getResources(),
                                R.drawable.print_ysf_refound);
                    } else {
                        bitmap = BitmapFactory.decodeResource(getResources(),
                                R.drawable.print_public);
                    }
//                    print(bitmap, content, Constants.PRINT_REFUND_ORDER, printpage);
                    PrinterWrapper.printNewRefundOrder(printpage,false,content);
                }
                break;

        }
    }

    private void print(Bitmap logo, String[] content, int orderType, int printPage) {

        printOrder(logo, content, orderType, printPage);
    }

    private void printOrder(Bitmap logo, String[] content, int orderType, int printPage) {
        PrinterWrapper.printPosOrder(logo, content, orderType, printPage);
    }

}
